#include  "board.h"
#include  "Typedef.h"
#include  "macros.h"

#include  "hal_mcu.h"
#include  "hal_avr.h"
#include  "hal_int.h"
#include  "hal_uart.h"

#include  "CC2500.h"
#include  "CC1100.h"
#include  "Timer.h"

#include  "Phy_Frame.h"
#include  "GlobalMethod.h"


//----------------------------------------------------------------------------------
//  Constants used in this file
//----------------------------------------------------------------------------------


//----------------------------------------------------------------------------------
//  Debug data used in this file
//----------------------------------------------------------------------------------

/*
INT8U  TempVendorID[] = { 0x12 , 0x34};   //VendorID
INT8U  TempeSealID[] = { 0xab , 0xbc , 0xcd , 0xde};   //eSealID

INT8U  TempDataType;
INT8U  TempDataLth;
INT8U  TempData[]={0x00};

INT8U  Debug = 1;
*/

#define   LF_WAKEUP_CHANNEL     (0x05)
#define   UHF_WAKEUP_CHANNEL    (0x09)

#define   CC1100_RX_TEST        0
#define   CC1100_TX_TEST        1

#define   CC2500_RX_TEST        0
#define   CC2500_TX_TEST        1

#define   CC2500_RX_PRINT       1
#define   CC1100_RX_PRINT       1

#define   RF_MAX_PARKET_SIZE    50
#define   RF_SEND_TIME_PERIOD   100

PHY_FRAME   RF_Tx_Parket;
PHY_RX_FRAME  RF_Rx_Parket;


void  Build_TestPartket(INT8U Length)
{
    INT8U i;
    for(i = 0 ; i < Length ; i++)
    {
        RF_Tx_Parket.PhyFrame[i] = i;
    }
    RF_Tx_Parket.FrameLength = Length;
}

__C_task  main( void )
{
   INT8U  CC2500_SendCltTimerID , CC1100_SendCltTimerID;
   INT8U  err;
   INT8U  CC2500_ParketLength , CC1100_ParketLength;
   INT8U  Str[4];
   INT8U  CC1100_Rx_Cnt , CC1100_Tx_Cnt;
   INT8U  CC2500_Rx_Cnt , CC2500_Tx_Cnt;
   INT8U  Rx_PrintOn = TRUE;
   halMcuInit();
   usart_init((INT32U)(115200));

   Timer_Init();

   CC1100_Init();
   CC2500_Init();

   CC1100_RxOn();
   CC2500_RxOn();

   CC2500_SetLogicalChannel(UHF_WAKEUP_CHANNEL);
   CC2500_SetTxPower(CC2500_TX_PA__8DBM);
   CC1100_SetLogicalChannel(LF_WAKEUP_CHANNEL);
   CC1100_SetTxPower(CC1100_TX_PA__5DBM);

   HAL_INT_ON();

   uart_write_line("System start");
   CC2500_SendCltTimerID = Timer_Apply(RF_SEND_TIME_PERIOD , &err);
   CC1100_SendCltTimerID = Timer_Apply(RF_SEND_TIME_PERIOD , &err);
   CC1100_Tx_Cnt = 0;
   CC1100_Rx_Cnt = 0;
   CC2500_Tx_Cnt = 0;
   CC2500_Rx_Cnt = 0;
#if CC2500_TX_TEST == 1
   StartTimer(CC2500_SendCltTimerID);
#endif
#if CC1100_TX_TEST == 1
   StartTimer(CC1100_SendCltTimerID);
#endif
   CC2500_ParketLength = 1;
   CC1100_ParketLength = 1;
   while(TRUE)
   {
       if(isTimeOver(CC2500_SendCltTimerID))
       {
           if(CC2500_ParketLength++ > RF_MAX_PARKET_SIZE)
           {
               CC2500_ParketLength = 1;
           }
           Build_TestPartket(CC2500_ParketLength);
           StopTimer(CC2500_SendCltTimerID);
           CC2500_Transmit(GET_TX_PHY_FRAME_ADDR(RF_Tx_Parket), GET_TX_PHY_FRAME_LTH(RF_Tx_Parket),CC2500_TX_TYPE_RANDOM);
           Hex2Str(&CC2500_ParketLength , Str , 1);
           uart_write_line("\n\r CC2500 send parket No.");


           uart_write_line(Str);
           uart_write_line("\n\r");
           StartTimer(CC2500_SendCltTimerID);
       }
       if(isTimeOver(CC1100_SendCltTimerID))
       {
           if(CC1100_ParketLength++ > RF_MAX_PARKET_SIZE)
           {
               CC1100_ParketLength = 1;
           }
           Build_TestPartket(CC1100_ParketLength);
           StopTimer(CC1100_SendCltTimerID);
           CC1100_Transmit(GET_TX_PHY_FRAME_ADDR(RF_Tx_Parket), GET_TX_PHY_FRAME_LTH(RF_Tx_Parket),CC1100_TX_TYPE_RANDOM);
           Hex2Str(&CC1100_ParketLength , Str , 1);
           uart_write_line("\n\r CC1100 send parket No.");
           uart_write_line(Str);
           uart_write_line("\n\r");
           StartTimer(CC1100_SendCltTimerID);
       }
#if  CC1100_RX_TEST
       if(CC1100_rxRecvPacket(RF_Rx_Parket.PhyFrame , &RF_Rx_Parket.FrameLength , RF_Rx_Parket.rxMetrics) == CC1100_RX_OK)
       {
#if  CC1100_RX_PRINT == 1
           if(Rx_PrintOn)
           {
               uart_write_line("\n\rCC1100 Rx Parket length:");
               Hex2Str(&RF_Rx_Parket.FrameLength , Str , 1);
               uart_write_line(Str);
               uart_write_line("\n\r Content:");
               for(INT8U i = 0 ; i < RF_Rx_Parket.FrameLength ; i++)
               {
                   Hex2Str(&RF_Rx_Parket.PhyFrame[i] , Str , 1);
                   uart_write_line(Str);
                   uart_write_line(" ");
               }
               uart_write_line("\n\r");
           }
#endif
           CC1100_Tx_Cnt = RF_Rx_Parket.FrameLength;
           CC1100_Rx_Cnt++;
           if(CC1100_Tx_Cnt == 1)
           {
               CC1100_Rx_Cnt = 1;
           }
           else if(CC1100_Tx_Cnt == RF_MAX_PARKET_SIZE)
           {
               uart_write_line("\n\r CC1100 Tx Num:");
               Hex2Str(&CC1100_Tx_Cnt , Str , 1);
               uart_write_line(Str);
               uart_write_line("\n\r CC1100 Rx Num:");
               Hex2Str(&CC1100_Rx_Cnt , Str , 1);
               uart_write_line(Str);
               CC1100_Rx_Cnt = 0;
               CC1100_Tx_Cnt = 0;
               uart_write_line("\n\r");
           }
       }
#endif
#if CC2500_RX_TEST
       if(CC2500_rxRecvPacket(RF_Rx_Parket.PhyFrame , &RF_Rx_Parket.FrameLength , RF_Rx_Parket.rxMetrics) == CC2500_RX_OK)
       {
#if  CC2500_RX_PRINT == 1
           if(Rx_PrintOn)
           {
               uart_write_line("\n\rCC2500 Rx Parket length:");
               Hex2Str(&RF_Rx_Parket.FrameLength , Str , 1);
               uart_write_line(Str);
               uart_write_line("\n\r Content:");
               for(INT8U i = 0 ; i < RF_Rx_Parket.FrameLength ; i++)
               {
                   Hex2Str(&RF_Rx_Parket.PhyFrame[i] , Str , 1);
                   uart_write_line(Str);
                   uart_write_line(" ");
               }
               uart_write_line("\n\r");
           }
#endif
           CC2500_Tx_Cnt = RF_Rx_Parket.FrameLength;
           CC2500_Rx_Cnt++;
           if(CC2500_Tx_Cnt == 1)
           {
               CC2500_Rx_Cnt = 1;
           }
           else if(CC2500_Tx_Cnt == RF_MAX_PARKET_SIZE)
           {
               uart_write_line("\n\r CC2500 Tx Num:");
               Hex2Str(&CC2500_Tx_Cnt , Str , 1);
               uart_write_line(Str);
               uart_write_line("\n\r CC2500 Rx Num:");
               Hex2Str(&CC2500_Rx_Cnt , Str , 1);
               uart_write_line(Str);
               CC2500_Rx_Cnt = 0;
               CC2500_Tx_Cnt = 0;
               uart_write_line("\n\r");
           }
       }
#endif
       if(usart_ischarinrxfifo())
       {
           INT8U TmpByte;
           TmpByte = usart_getchar();
           if( TmpByte == 's')
           {
               Rx_PrintOn = TRUE;
           }
           if( TmpByte == 'p')
           {
               Rx_PrintOn = FALSE;
           }
       }
   }
}
